<template>
  <div class="app-container">
    <el-form v-show="showSearch" ref="queryRef" :inline="true" :model="queryParams" label-width="90px">
      <el-form-item label="预约人姓名" prop="name" style="width: 300px;">
        <el-input v-model="queryParams.name" clearable placeholder="请输入" @keyup.enter="handleQuery"/>
      </el-form-item>
      <el-form-item label="预约人手机号" label-width="110px" prop="mobile">
        <el-input v-model="queryParams.mobile" clearable placeholder="请输入预约人手机号" @keyup.enter="handleQuery"/>
      </el-form-item>
      <el-form-item label="预约时间" prop="time">
        <el-date-picker v-model="queryParams.time" clearable placeholder="请选择预约时间" type="date"
                        value-format="YYYY-MM-DD"/>
      </el-form-item>

      <el-form-item>
        <el-button icon="Search" type="primary" @click="handleQuery">搜索</el-button>
        <el-button icon="Refresh" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-table v-loading="loading" :data="reservationList">
      <el-table-column align="center" label="序号" type="index" width="100px"/>
      <el-table-column align="center" label="预约人姓名" prop="name"/>
      <el-table-column align="center" label="预约人手机号" prop="mobile"/>
      <el-table-column align="center" label="预约时间" prop="time" width="180">
        <template #default="scope">
          <span>{{ parseTime(scope.row.time, '{y}-{m}-{d} {h}:{i}:{s}') }}</span>
        </template>
      </el-table-column>
      <el-table-column align="center" label="探访人" prop="visitor"/>
      <el-table-column align="center" label="预约类型" prop="type">
        <template #default="scope">
          <span>{{ scope.row.type === 0 ? '参观预约' : '探访预约' }}</span>
        </template>
      </el-table-column>
      <el-table-column align="center" label="预约状态" prop="status">
        <template #default="scope">
      <span>
        {{
          scope.row.status === 0 ? '待报道' : scope.row.status === 1 ? '已完成' : scope.row.status === 2 ? '取消' : '过期'
        }}
      </span>
        </template>
      </el-table-column>
      <el-table-column align="center" label="创建时间" prop="createTime" width="180">
        <template #default="scope">
          <span>{{ parseTime(scope.row.createTime, '{y}-{m}-{d} {h}:{i}:{s}') }}</span>
        </template>
      </el-table-column>
      <el-table-column align="center" class-name="small-padding fixed-width" fixed="right" label="操作" width="200">
        <template #default="scope">
          <el-button icon="Edit" link type="primary" @click="handleUpdate(scope.row)">修改</el-button>
          <el-button icon="ZoomIn" link type="primary" @click="handleLook(scope.row)">查看</el-button>
          <el-button icon="Delete" link type="primary" @click="handleDelete(scope.row)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination v-show="total > 0" v-model:limit="queryParams.pageSize" v-model:page="queryParams.pageNum"
                :total="total" @pagination="getList"/>

    <el-dialog v-model="dialogVisible" :title="title" append-to-body width="500px" @close="cancel()">
      <el-form ref="reservationRef" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="预约人姓名" prop="name">
          <el-input v-model="form.name" :disabled="isLook" placeholder="请输入预约人姓名"/>
        </el-form-item>
        <el-form-item label="预约人手机号" prop="mobile">
          <el-input v-model="form.mobile" :disabled="isLook" placeholder="请输入预约人手机号"/>
        </el-form-item>
        <el-form-item label="预约时间" prop="time">
          <el-date-picker v-model="form.time" :disabled="isLook" clearable placeholder="请选择预约时间" type="datetime"
                          value-format="YYYY-MM-DD HH:mm:ss"/>
        </el-form-item>
        <el-form-item label="探访人" prop="visitor">
          <el-input v-model="form.visitor" :disabled="isLook" placeholder="请输入探访人"/>
        </el-form-item>
      </el-form>
      <template #footer>
        <div v-if="!isLook" class="dialog-footer">
          <el-button type="primary" @click="submitForm">确 定</el-button>
          <el-button @click="cancel">取 消</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script name="Reservation" setup>
import {
  addReservation,
  delReservation,
  getReservation,
  listReservation,
  updateReservation
} from "@/api/nursing/reservation";

const {proxy} = getCurrentInstance();

const reservationList = ref([]);
const open = ref(false);
const loading = ref(true);
const showSearch = ref(true);
const ids = ref([]);
const total = ref(0);
const title = ref("");

const data = reactive({
  form: {},
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    name: null,
    mobile: null,
    time: null,
    visitor: null,
    type: null,
    status: null,
  },
  rules: {
    name: [
      {required: true, message: "预约人姓名不能为空", trigger: "blur"},
      {min: 1, max: 50, message: "预约人姓名长度必须介于 1 和 50 之间", trigger: "blur"}
    ],
    mobile: [
      {required: true, message: "预约人手机号不能为空", trigger: "blur"},
      {min: 11, max: 20, message: "预约人手机号长度必须介于 11 和 20 之间", trigger: "blur"}
    ],
    time: [{required: true, message: "预约时间不能为空", trigger: "blur"}],
    visitor: [
      {required: true, message: "探访人不能为空", trigger: "blur"},
      {min: 1, max: 50, message: "探访人长度必须介于 1 和 50 之间", trigger: "blur"}
    ]
  }
});

const {queryParams, form, rules} = toRefs(data);

/** 查询预约信息列表 */
function getList() {
  loading.value = true;
  listReservation(queryParams.value).then(response => {
    reservationList.value = response.rows;
    total.value = response.total;
    loading.value = false;
  });
}

// 取消按钮
function cancel() {
  dialogVisible.value = false;
  isLook.value = false;
  reset();
}

// 表单重置
function reset() {
  form.value = {
    id: null,
    name: null,
    mobile: null,
    time: null,
    visitor: null,
    type: null,
    status: null,
    createTime: null,
    updateTime: null,
    createBy: null,
    updateBy: null,
    remark: null
  };
  proxy.resetForm("reservationRef");
}

/** 搜索按钮操作 */
function handleQuery() {
  queryParams.value.pageNum = 1;
  getList();
}

/** 重置按钮操作 */
function resetQuery() {
  proxy.resetForm("queryRef");
  handleQuery();
}

/** 修改按钮操作 */
function handleUpdate(row) {
  const _id = row.id || ids.value
  getDetails(_id);
}

/** 提交按钮 */
function submitForm() {
  proxy.$refs["reservationRef"].validate(valid => {
    if (valid) {
      if (form.value.id != null) {
        updateReservation(form.value).then(response => {
          proxy.$modal.msgSuccess("修改成功");
          open.value = false;
          getList();
        });
      } else {
        addReservation(form.value).then(response => {
          proxy.$modal.msgSuccess("新增成功");
          open.value = false;
          getList();
        });
      }
    }
  });
}

/** 删除按钮操作 */
function handleDelete(row) {
  const _ids = row.id || ids.value;
  proxy.$modal.confirm('是否确认删除预约信息编号为"' + _ids + '"的数据项？').then(function () {
    return delReservation(_ids);
  }).then(() => {
    getList();
    proxy.$modal.msgSuccess("删除成功");
  }).catch(() => {
  });
}

// 获取详情
const getDetails = (id) => {
  getReservation(id).then((response) => {
    form.value = response.data;
    dialogVisible.value = true;
    if (isLook.value) {
      title.value = '查看预约信息';
    } else {
      title.value = form.value.id ? '修改预约信息' : '新增预约信息';
    }
  });
};

const isLook = ref(false);
const dialogVisible = ref(false);

// 查看
const handleLook = (row) => {
  isLook.value = true;
  dialogVisible.value = true;
  getDetails(row.id);
  title.value = '查看预约信息';
};

getList();
</script>
